<template>
  <div class="container_edit consultant">
    <div class="header">
      <p>常见问题</p>
    </div>
    <div class="main">
      <el-form
        :model="user"
        :rules="rules"
        ref="user"
        label-width="150px"
        class=""
      >
        <div
          v-for="(item,index) in user.list"
          :key="index"
          class="card-item"
        >
          <i
            @click="deleteItem(index)"
            class="el-icon-delete product-delete"
          ></i>
          <div class="main-item box">
            <div class="main-list">
               <el-form-item
                label="问题："
                prop="question"
              >
               <el-input
                  type="textarea"
                  v-model="item.question"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="回答："
                prop="answer"
              >
               <el-input
                  v-model="item.answer"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </div>
          </div>
        </div>
        <el-button
          style="margin:0 0 30px 60px;"
          type="primary"
          @click="addPerson"
        > 添加常见问题</el-button>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSave('user')"
          >保存</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");
export default {
  data() {
    return {
      id: "",
      currentPage: 1,
      user: {
        familyId: "",
        id: "",
        list: [
          {
            answer: "",
            question: ""
          }
        ]
      },
      rules: {
        // familyName: [
        //   { required: true, message: "请输入家庭名称", trigger: "blur" }
        // ],
        // policyOfficerName: [
        //   { required: true, message: "请输入保单管理员", trigger: "blur" }
        // ],
        // sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        // mobile: [{ required: true, message: "请输入手机", trigger: "blur" }],
        // birthday: [{ required: true, message: "请选择生日", trigger: "blur" }]
      },
    };
  },
  mounted() {
   this.getInfo();
  },
  methods: {

    deleteItem(index) {
      this.user.list.splice(index, 1);
    },
    addPerson() {
      this.user.list.push({
        answer: "",
        question: "",
      });
    },
    getInfo: function() {
      let url = CONSTANT.URL.LIST.question;
      common.get(url, '', res => {
        if (res.status == 200) {
          let data = res.data.bussData;
          this.user.list = data
        }
      });
    },
    editorInfo(val) {
      this.user.detail = val;
    },
    handleSave: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let url = CONSTANT.URL.LIST.questionSave;
          common.requestAjax(url, JSON.stringify(this.user), null, res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "保存成功！"
              });
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    beforeAvatarUpload(file) {
      let url = CONSTANT.URL.SYSTEM.UPLOADFILEBYCOS;
      let param = {
        suffix: file.name.split(".")[1], // 文件后缀  png
        contentType: file.type // 文件类型  image/png
      };
      common.requestAjax(url, param, null, res => {
        this.upLoadUrl = res.data.bussData.uploadUrl;
        this.user.avatarFileKey = res.data.bussData.fileKey;
        let downloadUrl = res.data.bussData.downloadUrl;
        common.uploadFile(this.upLoadUrl, file, file.type, res => {
          this.imageUrl = downloadUrl;
        });
      });
    },
    beforeAvatarUpload1(file) {
      let url = CONSTANT.URL.SYSTEM.UPLOADFILEBYCOS;
      let param = {
        suffix: file.name.split(".")[1], // 文件后缀  png
        contentType: file.type // 文件类型  image/png
      };
      common.requestAjax(url, param, null, res => {
        this.upLoadUrl = res.data.bussData.uploadUrl;
        this.user.qrCode = res.data.bussData.fileKey;
        let downloadUrl = res.data.bussData.downloadUrl;
        common.uploadFile(this.upLoadUrl, file, file.type, res => {
          this.imageUrl1 = downloadUrl;
        });
      });
    },
    avatarUpload(file, index) {
      let url = CONSTANT.URL.SYSTEM.UPLOADFILEBYCOS;
      let param = {
        suffix: file.name.split(".")[1], // 文件后缀  png
        contentType: file.type // 文件类型  image/png
      };
      common.requestAjax(url, param, null, res => {
        this.upLoadUrl = res.data.bussData.uploadUrl;
        this.user.list[index].pictureFileKey = res.data.bussData.fileKey;
        let downloadUrl = res.data.bussData.downloadUrl;
        common.uploadFile(this.upLoadUrl, file, file.type, res => {
          this.user.list[index].pictureUrl = downloadUrl;
        });
      });
    },
    back() {
      this.$router.push({
        path: "/planList",
        query: { currentPage: this.currentPage }
      });
    }
  }
};
</script>
<style lang="scss">
.consultant {
  .city {
    .el-select,
    .el-input {
      width: 123px;
    }
  }
  .product-delete {
    position: absolute;
    right: 20px;
    top: 20px;
    z-index: 1001;
    font-size: 20px;
    cursor: pointer;
  }
  .card-item {
    position: relative;
    width: 85%;
    border: 1px dotted #bbb;
    padding-top: 30px;
    margin-bottom: 20px;
    .el-input {
      width: 80%;
    }
  }
}
</style>
